//
//  TGGaodeLocation.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/9/5.
//

#import "TGGaodeLocation.h"
#import "LBXPermission.h"
#import "LBXPermissionSetting.h"

@implementation TGGaodeLocation

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static TGGaodeLocation * _location = nil;
    dispatch_once(&onceToken, ^{
        _location = [[self alloc] init];
    });
    return _location;
}

- (instancetype)init {
    if (self = [super init]) {
        [self configLocationManager];
    }
    return self;
}

- (void)configLocationManager {
    
    [AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
    [AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
    self.locationManager = [[AMapLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    //设置不允许系统暂停定位
     [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
//    [self.locationManager setAllowsBackgroundLocationUpdates:YES];

    [self.locationManager setLocationTimeout:LocationTimeout];
    
    [self.locationManager setReGeocodeTimeout:ReGeocodeTimeout];
    
}

- (void)cleanUpAction {
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];
}

- (void)geocodeAction:(AMapLocatingCompletionBlock)block {
    if (self.locationManager == nil) {
        [self configLocationManager];
    }else {
        [self.locationManager stopUpdatingLocation];
    }
    self.completionBlock = block;
    [self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
}

- (void)reGeocodeAction:(AMapLocatingCompletionBlock)block {
    
    [LBXPermission authorizeWithType:LBXPermissionType_Location completion:^(BOOL granted, BOOL firstTime) {
        if (granted) {
            if (self.locationManager == nil) {
                [self configLocationManager];
            }else {
                [self.locationManager stopUpdatingLocation];
            }
            self.completionBlock = block;
            [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
        } else if(!firstTime) {
            [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"提示" msg:@"您好，需要获取您当前的位置为照片添加水印" cancel:@"暂不打开" setting:@"设置" ];
        }
        
    }];
   
}

+ (NSString *)distanceOfPoint:(CLLocationCoordinate2D)location1 point2:(CLLocationCoordinate2D)location2 {
     NSString * k = nil;

    CLLocation *first = [[CLLocation alloc] initWithLatitude:location1.latitude longitude:location1.longitude];
    CLLocation *second = [[CLLocation alloc] initWithLatitude:location2.latitude longitude:location2.longitude];
    CLLocationDistance distance = [second distanceFromLocation:first];
    
    if (distance >=1000) {
        k = [NSString stringWithFormat:@"%.1fkm",distance/1000];
    }else{
        if (distance < 100){
            k = @"<100m";
        }else{
            k = [NSString stringWithFormat:@"%.1fm",distance];
        }
    }
    return k;
//    MAMapPoint point1 = MAMapPointForCoordinate(location1);
//    MAMapPoint point2 = MAMapPointForCoordinate(location2);
//    CLLocationDistance distance = MAMetersBetweenMapPoints(point1,point2);
//    if (distance >=1000) {
//        k = [NSString stringWithFormat:@"%.1fkm",distance/1000];
//    }else{
//        if (distance < 100){
//            k = @"<100m";
//        }else{
//            k = [NSString stringWithFormat:@"%.1fm",distance];
//        }
//    }
//    NSLog(@"distance = %@",k);
//    return k;
}

- (void)maintenanceGeocodeAction:(AMapLocatingCompletionBlock)block {
    
    [LBXPermission authorizeWithType:LBXPermissionType_Location completion:^(BOOL granted, BOOL firstTime) {
        if (granted) {
            if (self.locationManager == nil) {
                [self configLocationManager];
            }else {
                [self.locationManager stopUpdatingLocation];
            }
            self.completionBlock = block;
            [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
        } else if(!firstTime) {
            [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"提示" msg:@"打开“定位服务”来允许“通冠易租确定你的位置”" cancel:@"取消" setting:@"设置" ];
        }
        
    }];
}

+ (NSString *)maintenancedistanceOfPoint:(CLLocation*)location1 point2:(CLLocationCoordinate2D)location2 {
     NSString * k = nil;

//    CLLocation *first = [[CLLocation alloc] initWithLatitude:location1.latitude longitude:location1.longitude];
    CLLocation *second = [[CLLocation alloc] initWithLatitude:location2.latitude longitude:location2.longitude];
    CLLocationDistance distance = [second distanceFromLocation:location1];
    
    if (distance >=1000) {
        k = [NSString stringWithFormat:@"%.2fkm",distance/1000];
    }else{
        if (distance < 100){
            k = @"<100m";
        }else{
            k = [NSString stringWithFormat:@"%.2fm",distance];
        }
    }
    return k;
//    MAMapPoint point1 = MAMapPointForCoordinate(location1);
//    MAMapPoint point2 = MAMapPointForCoordinate(location2);
//    CLLocationDistance distance = MAMetersBetweenMapPoints(point1,point2);
//    if (distance >=1000) {
//        k = [NSString stringWithFormat:@"%.1fkm",distance/1000];
//    }else{
//        if (distance < 100){
//            k = @"<100m";
//        }else{
//            k = [NSString stringWithFormat:@"%.1fm",distance];
//        }
//    }
//    NSLog(@"distance = %@",k);
//    return k;
}
@end
